﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DietPlanning.Models;

namespace DietPlanning.Controllers
{
    public class DishController : Controller
    {
        private DailyFoodEntities db = new DailyFoodEntities();

        //
        // GET: /Dish/

        public ViewResult Index()
        {
            var dishes = db.Dishes.Include("DishType");
            return View(dishes.ToList());
        }

        //
        // GET: /Dish/Details/5

        public ViewResult Details(int id)
        {
            Dish dish = db.Dishes.Single(d => d.DishID == id);
            return View(dish);
        }

        //
        // GET: /Dish/Create

        public ActionResult Create()
        {
            ViewBag.DishTypeId = new SelectList(db.DishTypes, "DishTypeId", "DishTypeName");
            return View();
        }

        //
        // POST: /Dish/Create

        [HttpPost]
        public ActionResult Create(Dish dish)
        {
            if (ModelState.IsValid)
            {
                db.Dishes.AddObject(dish);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.DishTypeId = new SelectList(db.DishTypes, "DishTypeId", "DishTypeName", dish.DishTypeId);
            return View(dish);
        }

        //
        // GET: /Dish/Edit/5

        public ActionResult Edit(int id)
        {
            Dish dish = db.Dishes.Single(d => d.DishID == id);
            ViewBag.DishTypeId = new SelectList(db.DishTypes, "DishTypeId", "DishTypeName", dish.DishTypeId);
            return View(dish);
        }

        //
        // POST: /Dish/Edit/5

        [HttpPost]
        public ActionResult Edit(Dish dish)
        {
            if (ModelState.IsValid)
            {
                db.Dishes.Attach(dish);
                db.ObjectStateManager.ChangeObjectState(dish, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.DishTypeId = new SelectList(db.DishTypes, "DishTypeId", "DishTypeName", dish.DishTypeId);
            return View(dish);
        }

        //
        // GET: /Dish/Delete/5

        public ActionResult Delete(int id)
        {
            Dish dish = db.Dishes.Single(d => d.DishID == id);
            //            return View(dish);
            db.Dishes.DeleteObject(dish);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        // POST: /Dish/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Dish dish = db.Dishes.Single(d => d.DishID == id);
            db.Dishes.DeleteObject(dish);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        //
        // POST: /Dish/LoadFavorite/5


        public ActionResult DishPage()
        {
            //var favoriteList = (from e in db.Dishes
            //              select e).OrderByDescending(o=>o.AverageRate).Take(10).ToList();
            //return View(favoriteList);

            var dishes = db.Dishes.Include("DishType");
            return View(dishes.ToList());
        }
        [HttpPost]
        public ActionResult DishPage(String dishName)
        {
            //var favoriteList = (from e in db.Dishes
            //              select e).OrderByDescending(o=>o.AverageRate).Take(10).ToList();
            //return View(favoriteList);
            //dishName = Request["txtDishName"];
            var dishes = db.Dishes.Include("DishType");
            return View(dishes.ToList());
        }

        public ActionResult ManageDish()
        {
            //Get userId
            var userId = Int32.Parse(Session["UserId"].ToString());
            var dishes = db.Dishes.Include("DishType").Where(o => o.UserID == userId);
            return View(dishes.ToList());
        }

        public ViewResult ViewDish(int id)
        {
            Dish dish = db.Dishes.Single(d => d.DishID == id);
            return View(dish);
        }
    }
}